﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Timers;
using System.Diagnostics;
using XNADota.Graphics;

namespace XNADota.GamePlay
{
    public enum EGameplayPhase
    {
        SelectHero,
        Battle,
        End,
    }

    public class GamePhaseManager
    {
        private GamePhase _CurrentPhase;

        public GamePhaseManager()
        {
            NextPhase(EGameplayPhase.Battle);
        }

        public void NextPhase(EGameplayPhase nextPhase)
        {
            if (_CurrentPhase != null)
                _CurrentPhase.EndPhase();

            switch (nextPhase)
            {
                case EGameplayPhase.SelectHero:
                    _CurrentPhase = new GamePhase(this);
                    break;
                case EGameplayPhase.Battle:
                    _CurrentPhase = new BattlePhase(this);
                    break;
                case EGameplayPhase.End:
                    _CurrentPhase = new GamePhase(this);
                    break;
            }

            _CurrentPhase.StartPhase();
        }

        public virtual void Update(GameTime gameTime)
        {
            _CurrentPhase.Update(gameTime);
        }

        public void UpdateAfterIntegration()
        {
            _CurrentPhase.UpdateAfterIntegration();
        }

        public virtual void Draw(GameTime gameTime)
        {
            _CurrentPhase.Draw(gameTime);
        }
    }


    public class BattlePhase : GamePhase
    {
        public const int MaxSoliderCount = 1000;
        private UnitObject[] _Soliders;
        private TimeSpan _BattleClock;

        public BattlePhase(GamePhaseManager manager)
            : base(manager)
        {
            _Soliders = new UnitObject[MaxSoliderCount];
            _BattleClock = new TimeSpan(0, 0, 0);

            // debugbdw
            GenerateSoliders();
        }

        public override void Update(GameTime gameTime)
        {
            _BattleClock = _BattleClock.Add(gameTime.ElapsedGameTime);

            for (int i = 0; i < 1; i++)
            {
                _Soliders[i].Update(gameTime);
            }

            base.Update(gameTime);
        }

        public override void UpdateAfterIntegration()
        {
            for (int i = 0; i < 1; i++)
                _Soliders[i].UpdateAfterPhysicsIntergration();

            base.UpdateAfterIntegration();
        }

        public void GenerateSoliders()
        {
            for (int i = 0; i < 1; i++)
            {
                _Soliders[i] = GameplayWorld.Instance.ObjectsManager.CreateObject("esen", EObjectType.UnitData) as UnitObject;
                _Soliders[i].Position = GameplayWorld.Instance.PathManager.GetHeight(new Vector3(64 * 128, 0, 64 * 128));
                _Soliders[i].ApplyPosition();
                //_Soliders[0].Scale = new Vector3(1.0f, 1.0f, 1.0f);

                Debug.Assert(_Soliders[i] != null);

                SceneManager.Instance.AddInstance(_Soliders[i]);
                SceneManager.Instance.AddUpdateInstance(_Soliders[i]);
            }
        }
    }

    public class GamePhase
    {
        protected GamePhaseManager _GamePhaseManager;

        public GamePhase(GamePhaseManager manager)
        {
            _GamePhaseManager = manager;
        }

        public virtual void StartPhase()
        {
        }

        public virtual void EndPhase()
        {
        }

        public virtual void Update(GameTime gameTime)
        {
        }

        public virtual void UpdateAfterIntegration()
        {

        }

        public virtual void Draw(GameTime gameTime)
        {
        }
    }
}
